import Dingo.API as api
import Dingo.sound as sound
import random
import debug as SSdebug
from numarray import *
import numarray.linear_algebra as la
import wx
import OutWin
#import bleh

__author__ = 'John Kemp'

newBleh = bleh.BlehRFRanger()
newBleh.get_rssi((100, 100), (100, 200))

Colors = api.SensorColor
debug = 3

colours = {"Actuator":Colors.Red, "Listener":Colors.Green}

nodes = [(25, 50), (50, 10), (10, 10), (150, 50), (75, 10), (10, 100)]      # Actuators

display = OutWin.createDisplay(None, wx.ID_ANY, "Test")

class TheSimulation(api.Simulation):
    def __init__(self, graph):
        if debug: SSdebug.writeHeader()
        api.Ether.RF_radius = 400
        ether = api.Ether(self)
        sensors = (ASensor(graph, ether, i) for i in xrange(len(nodes)))
        api.Simulation.__init__(self, sensors, graph)
        sound.Beep.radius = 400 ### Be Pythonic!
        if debug: print ""
        return

    def run(self):
        super(TheSimulation, self).run()
        return


class APacket(api.Packet):
	def __init__(self, sender, ether, data):
		self.ether = ether
		api.Packet.__init__(self, sender, None, data)
		return


class ASensor(api.Sensor, sound.SoundSensor):
    def __init__(self, graph, ether, i):
        self.graph = graph
        self.i = i
        x, y = nodes[i][:2]
        if len(nodes[i]) == 2:
            self.state = "Actuator"
            self.x, self.y = x, y
        else:
            self.state = nodes[i][2:3][0]
            self.x, self.y = 0,0
            panel.lblAct.SetLabel("Sensor[%d] actual position : %d, %d" % (i, x, y))
        self.colour = colours[self.state]

        # --- Start editing ---

        self.dists = [0.0 for j in xrange(3)]
        self.coords = [[0.0, 0.0] for j in xrange(3)]
        self.currGet = 0
        
        #  --- Stop editing ---

        sched = api.Scheduler([ATask(self), sound.Microphone(1, 0, self)], -1)
        api.Sensor.__init__(self, graph, "Sensor[%d]" % i, (x, y), self.colour, sched, ether)
        sound.SoundSensor.__init__(self, "Sensor[%d]" % i, "/tmp/ksensor/")
        display.addSensor("Sensor[%d]" % i)
        display.addOutput(i, "Sensor[%d] created" % i)
        count = 0
        if debug: SSdebug.writeInfo(self, x, y)
        return


class ATask(api.Task):
    def __init__(self, sensor):
        sensor.tick = -1
        api.Task.__init__(self, 0, 0, sensor)

    def task(self):
        node = self.sensor.node
        sensor = self.sensor
        sensor.tick += 1
        # --- Start editing ---
        if sensor.tick % 50 == int(sensor.id[7:8][0]) * 5:
            packet = APacket(sensor.id, sensor.ether, [sensor.x, sensor.y])
            sensor.broadcast(api.SensorColor.FromName("Blue"), None, packet)
            display.addOutput(sensor.i, "Broadcast at tick %d" % sensor.tick)

        while not sensor.recv_q.empty():
            r_packet = sensor.recv_q.get()
            if debug == 3: display.addDebug(sensor.i, "Received : " + r_packet.__repr__())
            if debug == 2: display.addDebug(sensor.i, r_packet.sender_id + " : radio = %f" % r_packet.time_received)

        # --- Stop editing ---

        api.Sleep(0.2)                  # Give the UI a chance
        return
